{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.model_selection import GridSearchCV, StratifiedKFold\n",
    "from sklearn.preprocessing import RobustScaler\n",
    "from sklearn.impute import SimpleImputer, KNNImputer\n",
    "from sklearn.feature_selection import SelectKBest, f_classif\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.ensemble import RandomForestClassifier, VotingClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.neural_network import MLPClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('../Datasets/reduced_II.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>P_Id</th>\n",
       "      <th>PPG_Rate_Mean</th>\n",
       "      <th>HRV_SDNN</th>\n",
       "      <th>HRV_MadNN</th>\n",
       "      <th>HRV_SDRMSSD</th>\n",
       "      <th>HRV_Prc20NN</th>\n",
       "      <th>HRV_MinNN</th>\n",
       "      <th>HRV_TINN</th>\n",
       "      <th>HRV_LF</th>\n",
       "      <th>HRV_HF</th>\n",
       "      <th>...</th>\n",
       "      <th>HRV_MFDFA_alpha2_Fluctuation</th>\n",
       "      <th>HRV_ApEn</th>\n",
       "      <th>HRV_SampEn</th>\n",
       "      <th>HRV_ShanEn</th>\n",
       "      <th>HRV_MSEn</th>\n",
       "      <th>HRV_CMSEn</th>\n",
       "      <th>HRV_HFD</th>\n",
       "      <th>HRV_KFD</th>\n",
       "      <th>HRV_LZC</th>\n",
       "      <th>anxiety_meter</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>101</td>\n",
       "      <td>72.160656</td>\n",
       "      <td>161.144697</td>\n",
       "      <td>106.0059</td>\n",
       "      <td>0.744888</td>\n",
       "      <td>754.2</td>\n",
       "      <td>308.0</td>\n",
       "      <td>593.7500</td>\n",
       "      <td>0.016549</td>\n",
       "      <td>0.110477</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000557</td>\n",
       "      <td>0.905663</td>\n",
       "      <td>1.662548</td>\n",
       "      <td>7.103751</td>\n",
       "      <td>1.229793</td>\n",
       "      <td>1.392336</td>\n",
       "      <td>1.965384</td>\n",
       "      <td>4.166548</td>\n",
       "      <td>1.197715</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>102</td>\n",
       "      <td>94.333514</td>\n",
       "      <td>196.751139</td>\n",
       "      <td>152.7078</td>\n",
       "      <td>0.729774</td>\n",
       "      <td>519.4</td>\n",
       "      <td>307.0</td>\n",
       "      <td>320.3125</td>\n",
       "      <td>0.054757</td>\n",
       "      <td>0.067803</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000133</td>\n",
       "      <td>1.094277</td>\n",
       "      <td>1.868132</td>\n",
       "      <td>7.555224</td>\n",
       "      <td>1.174007</td>\n",
       "      <td>1.288954</td>\n",
       "      <td>1.967041</td>\n",
       "      <td>2.788036</td>\n",
       "      <td>1.117846</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>103</td>\n",
       "      <td>91.269287</td>\n",
       "      <td>240.470162</td>\n",
       "      <td>203.1162</td>\n",
       "      <td>0.766559</td>\n",
       "      <td>473.0</td>\n",
       "      <td>307.0</td>\n",
       "      <td>203.1250</td>\n",
       "      <td>0.050173</td>\n",
       "      <td>0.072260</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000077</td>\n",
       "      <td>0.962326</td>\n",
       "      <td>1.746639</td>\n",
       "      <td>7.316140</td>\n",
       "      <td>1.387296</td>\n",
       "      <td>1.315389</td>\n",
       "      <td>1.939231</td>\n",
       "      <td>4.690344</td>\n",
       "      <td>1.122028</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>104</td>\n",
       "      <td>76.862836</td>\n",
       "      <td>295.652405</td>\n",
       "      <td>182.3598</td>\n",
       "      <td>0.704668</td>\n",
       "      <td>600.6</td>\n",
       "      <td>309.0</td>\n",
       "      <td>414.0625</td>\n",
       "      <td>0.054656</td>\n",
       "      <td>0.072211</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000351</td>\n",
       "      <td>0.891039</td>\n",
       "      <td>1.377711</td>\n",
       "      <td>7.143425</td>\n",
       "      <td>1.196983</td>\n",
       "      <td>1.302091</td>\n",
       "      <td>1.974733</td>\n",
       "      <td>2.787734</td>\n",
       "      <td>1.051012</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>105</td>\n",
       "      <td>68.409940</td>\n",
       "      <td>655.450522</td>\n",
       "      <td>324.6894</td>\n",
       "      <td>0.712167</td>\n",
       "      <td>564.0</td>\n",
       "      <td>301.0</td>\n",
       "      <td>453.1250</td>\n",
       "      <td>0.040793</td>\n",
       "      <td>0.017122</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000442</td>\n",
       "      <td>1.097319</td>\n",
       "      <td>1.668343</td>\n",
       "      <td>7.084940</td>\n",
       "      <td>1.458232</td>\n",
       "      <td>1.302750</td>\n",
       "      <td>1.984928</td>\n",
       "      <td>2.193969</td>\n",
       "      <td>1.162819</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   P_Id  PPG_Rate_Mean    HRV_SDNN  HRV_MadNN  HRV_SDRMSSD  HRV_Prc20NN  \\\n",
       "0   101      72.160656  161.144697   106.0059     0.744888        754.2   \n",
       "1   102      94.333514  196.751139   152.7078     0.729774        519.4   \n",
       "2   103      91.269287  240.470162   203.1162     0.766559        473.0   \n",
       "3   104      76.862836  295.652405   182.3598     0.704668        600.6   \n",
       "4   105      68.409940  655.450522   324.6894     0.712167        564.0   \n",
       "\n",
       "   HRV_MinNN  HRV_TINN    HRV_LF    HRV_HF  ...  HRV_MFDFA_alpha2_Fluctuation  \\\n",
       "0      308.0  593.7500  0.016549  0.110477  ...                      0.000557   \n",
       "1      307.0  320.3125  0.054757  0.067803  ...                      0.000133   \n",
       "2      307.0  203.1250  0.050173  0.072260  ...                      0.000077   \n",
       "3      309.0  414.0625  0.054656  0.072211  ...                      0.000351   \n",
       "4      301.0  453.1250  0.040793  0.017122  ...                      0.000442   \n",
       "\n",
       "   HRV_ApEn  HRV_SampEn  HRV_ShanEn  HRV_MSEn  HRV_CMSEn   HRV_HFD   HRV_KFD  \\\n",
       "0  0.905663    1.662548    7.103751  1.229793   1.392336  1.965384  4.166548   \n",
       "1  1.094277    1.868132    7.555224  1.174007   1.288954  1.967041  2.788036   \n",
       "2  0.962326    1.746639    7.316140  1.387296   1.315389  1.939231  4.690344   \n",
       "3  0.891039    1.377711    7.143425  1.196983   1.302091  1.974733  2.787734   \n",
       "4  1.097319    1.668343    7.084940  1.458232   1.302750  1.984928  2.193969   \n",
       "\n",
       "    HRV_LZC  anxiety_meter  \n",
       "0  1.197715            7.0  \n",
       "1  1.117846           16.0  \n",
       "2  1.122028           10.0  \n",
       "3  1.051012            8.0  \n",
       "4  1.162819           15.0  \n",
       "\n",
       "[5 rows x 42 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(101, 42)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df.drop(columns=['anxiety_meter', 'P_Id'], axis=1)\n",
    "y = df['anxiety_meter']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PPG_Rate_Mean</th>\n",
       "      <th>HRV_SDNN</th>\n",
       "      <th>HRV_MadNN</th>\n",
       "      <th>HRV_SDRMSSD</th>\n",
       "      <th>HRV_Prc20NN</th>\n",
       "      <th>HRV_MinNN</th>\n",
       "      <th>HRV_TINN</th>\n",
       "      <th>HRV_LF</th>\n",
       "      <th>HRV_HF</th>\n",
       "      <th>HRV_VHF</th>\n",
       "      <th>...</th>\n",
       "      <th>HRV_MFDFA_alpha2_Asymmetry</th>\n",
       "      <th>HRV_MFDFA_alpha2_Fluctuation</th>\n",
       "      <th>HRV_ApEn</th>\n",
       "      <th>HRV_SampEn</th>\n",
       "      <th>HRV_ShanEn</th>\n",
       "      <th>HRV_MSEn</th>\n",
       "      <th>HRV_CMSEn</th>\n",
       "      <th>HRV_HFD</th>\n",
       "      <th>HRV_KFD</th>\n",
       "      <th>HRV_LZC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>72.160656</td>\n",
       "      <td>161.144697</td>\n",
       "      <td>106.0059</td>\n",
       "      <td>0.744888</td>\n",
       "      <td>754.2</td>\n",
       "      <td>308.0</td>\n",
       "      <td>593.7500</td>\n",
       "      <td>0.016549</td>\n",
       "      <td>0.110477</td>\n",
       "      <td>0.026186</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.339827</td>\n",
       "      <td>0.000557</td>\n",
       "      <td>0.905663</td>\n",
       "      <td>1.662548</td>\n",
       "      <td>7.103751</td>\n",
       "      <td>1.229793</td>\n",
       "      <td>1.392336</td>\n",
       "      <td>1.965384</td>\n",
       "      <td>4.166548</td>\n",
       "      <td>1.197715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>94.333514</td>\n",
       "      <td>196.751139</td>\n",
       "      <td>152.7078</td>\n",
       "      <td>0.729774</td>\n",
       "      <td>519.4</td>\n",
       "      <td>307.0</td>\n",
       "      <td>320.3125</td>\n",
       "      <td>0.054757</td>\n",
       "      <td>0.067803</td>\n",
       "      <td>0.018893</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.585898</td>\n",
       "      <td>0.000133</td>\n",
       "      <td>1.094277</td>\n",
       "      <td>1.868132</td>\n",
       "      <td>7.555224</td>\n",
       "      <td>1.174007</td>\n",
       "      <td>1.288954</td>\n",
       "      <td>1.967041</td>\n",
       "      <td>2.788036</td>\n",
       "      <td>1.117846</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>91.269287</td>\n",
       "      <td>240.470162</td>\n",
       "      <td>203.1162</td>\n",
       "      <td>0.766559</td>\n",
       "      <td>473.0</td>\n",
       "      <td>307.0</td>\n",
       "      <td>203.1250</td>\n",
       "      <td>0.050173</td>\n",
       "      <td>0.072260</td>\n",
       "      <td>0.013437</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.851135</td>\n",
       "      <td>0.000077</td>\n",
       "      <td>0.962326</td>\n",
       "      <td>1.746639</td>\n",
       "      <td>7.316140</td>\n",
       "      <td>1.387296</td>\n",
       "      <td>1.315389</td>\n",
       "      <td>1.939231</td>\n",
       "      <td>4.690344</td>\n",
       "      <td>1.122028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>76.862836</td>\n",
       "      <td>295.652405</td>\n",
       "      <td>182.3598</td>\n",
       "      <td>0.704668</td>\n",
       "      <td>600.6</td>\n",
       "      <td>309.0</td>\n",
       "      <td>414.0625</td>\n",
       "      <td>0.054656</td>\n",
       "      <td>0.072211</td>\n",
       "      <td>0.010139</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.542126</td>\n",
       "      <td>0.000351</td>\n",
       "      <td>0.891039</td>\n",
       "      <td>1.377711</td>\n",
       "      <td>7.143425</td>\n",
       "      <td>1.196983</td>\n",
       "      <td>1.302091</td>\n",
       "      <td>1.974733</td>\n",
       "      <td>2.787734</td>\n",
       "      <td>1.051012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>68.409940</td>\n",
       "      <td>655.450522</td>\n",
       "      <td>324.6894</td>\n",
       "      <td>0.712167</td>\n",
       "      <td>564.0</td>\n",
       "      <td>301.0</td>\n",
       "      <td>453.1250</td>\n",
       "      <td>0.040793</td>\n",
       "      <td>0.017122</td>\n",
       "      <td>0.000683</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000442</td>\n",
       "      <td>1.097319</td>\n",
       "      <td>1.668343</td>\n",
       "      <td>7.084940</td>\n",
       "      <td>1.458232</td>\n",
       "      <td>1.302750</td>\n",
       "      <td>1.984928</td>\n",
       "      <td>2.193969</td>\n",
       "      <td>1.162819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>106.643368</td>\n",
       "      <td>199.166962</td>\n",
       "      <td>64.4931</td>\n",
       "      <td>0.811141</td>\n",
       "      <td>461.6</td>\n",
       "      <td>332.0</td>\n",
       "      <td>218.7500</td>\n",
       "      <td>0.048237</td>\n",
       "      <td>0.067233</td>\n",
       "      <td>0.010414</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.712466</td>\n",
       "      <td>0.007395</td>\n",
       "      <td>0.789207</td>\n",
       "      <td>0.678190</td>\n",
       "      <td>6.976664</td>\n",
       "      <td>1.056822</td>\n",
       "      <td>1.177499</td>\n",
       "      <td>1.907768</td>\n",
       "      <td>2.744730</td>\n",
       "      <td>0.736001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>88.979798</td>\n",
       "      <td>429.343966</td>\n",
       "      <td>123.7971</td>\n",
       "      <td>0.832398</td>\n",
       "      <td>480.8</td>\n",
       "      <td>350.0</td>\n",
       "      <td>195.3125</td>\n",
       "      <td>0.068547</td>\n",
       "      <td>0.014395</td>\n",
       "      <td>0.000205</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.368860</td>\n",
       "      <td>0.017545</td>\n",
       "      <td>0.823860</td>\n",
       "      <td>0.770202</td>\n",
       "      <td>6.889927</td>\n",
       "      <td>0.837755</td>\n",
       "      <td>1.038622</td>\n",
       "      <td>1.897684</td>\n",
       "      <td>1.796485</td>\n",
       "      <td>1.018469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>109.566313</td>\n",
       "      <td>155.993383</td>\n",
       "      <td>50.4084</td>\n",
       "      <td>0.795211</td>\n",
       "      <td>469.0</td>\n",
       "      <td>309.0</td>\n",
       "      <td>250.0000</td>\n",
       "      <td>0.046989</td>\n",
       "      <td>0.094851</td>\n",
       "      <td>0.029186</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.708743</td>\n",
       "      <td>0.000805</td>\n",
       "      <td>0.856094</td>\n",
       "      <td>0.805586</td>\n",
       "      <td>7.184780</td>\n",
       "      <td>0.978753</td>\n",
       "      <td>1.191947</td>\n",
       "      <td>1.932554</td>\n",
       "      <td>2.855720</td>\n",
       "      <td>0.763747</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>126.073719</td>\n",
       "      <td>112.120465</td>\n",
       "      <td>34.8411</td>\n",
       "      <td>0.841414</td>\n",
       "      <td>427.0</td>\n",
       "      <td>302.0</td>\n",
       "      <td>171.8750</td>\n",
       "      <td>0.040691</td>\n",
       "      <td>0.030826</td>\n",
       "      <td>0.003372</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.893389</td>\n",
       "      <td>0.001636</td>\n",
       "      <td>0.783548</td>\n",
       "      <td>0.744972</td>\n",
       "      <td>6.726446</td>\n",
       "      <td>0.474042</td>\n",
       "      <td>1.009792</td>\n",
       "      <td>1.917123</td>\n",
       "      <td>1.941572</td>\n",
       "      <td>0.839453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>95.685516</td>\n",
       "      <td>66.593056</td>\n",
       "      <td>59.3040</td>\n",
       "      <td>0.956355</td>\n",
       "      <td>572.8</td>\n",
       "      <td>388.0</td>\n",
       "      <td>320.3125</td>\n",
       "      <td>0.020380</td>\n",
       "      <td>0.034959</td>\n",
       "      <td>0.001453</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.175111</td>\n",
       "      <td>0.000817</td>\n",
       "      <td>1.036170</td>\n",
       "      <td>1.528139</td>\n",
       "      <td>6.962487</td>\n",
       "      <td>1.326621</td>\n",
       "      <td>1.389550</td>\n",
       "      <td>1.857610</td>\n",
       "      <td>2.694537</td>\n",
       "      <td>0.890041</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>101 rows × 40 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     PPG_Rate_Mean    HRV_SDNN  HRV_MadNN  HRV_SDRMSSD  HRV_Prc20NN  \\\n",
       "0        72.160656  161.144697   106.0059     0.744888        754.2   \n",
       "1        94.333514  196.751139   152.7078     0.729774        519.4   \n",
       "2        91.269287  240.470162   203.1162     0.766559        473.0   \n",
       "3        76.862836  295.652405   182.3598     0.704668        600.6   \n",
       "4        68.409940  655.450522   324.6894     0.712167        564.0   \n",
       "..             ...         ...        ...          ...          ...   \n",
       "96      106.643368  199.166962    64.4931     0.811141        461.6   \n",
       "97       88.979798  429.343966   123.7971     0.832398        480.8   \n",
       "98      109.566313  155.993383    50.4084     0.795211        469.0   \n",
       "99      126.073719  112.120465    34.8411     0.841414        427.0   \n",
       "100      95.685516   66.593056    59.3040     0.956355        572.8   \n",
       "\n",
       "     HRV_MinNN  HRV_TINN    HRV_LF    HRV_HF   HRV_VHF  ...  \\\n",
       "0        308.0  593.7500  0.016549  0.110477  0.026186  ...   \n",
       "1        307.0  320.3125  0.054757  0.067803  0.018893  ...   \n",
       "2        307.0  203.1250  0.050173  0.072260  0.013437  ...   \n",
       "3        309.0  414.0625  0.054656  0.072211  0.010139  ...   \n",
       "4        301.0  453.1250  0.040793  0.017122  0.000683  ...   \n",
       "..         ...       ...       ...       ...       ...  ...   \n",
       "96       332.0  218.7500  0.048237  0.067233  0.010414  ...   \n",
       "97       350.0  195.3125  0.068547  0.014395  0.000205  ...   \n",
       "98       309.0  250.0000  0.046989  0.094851  0.029186  ...   \n",
       "99       302.0  171.8750  0.040691  0.030826  0.003372  ...   \n",
       "100      388.0  320.3125  0.020380  0.034959  0.001453  ...   \n",
       "\n",
       "     HRV_MFDFA_alpha2_Asymmetry  HRV_MFDFA_alpha2_Fluctuation  HRV_ApEn  \\\n",
       "0                     -0.339827                      0.000557  0.905663   \n",
       "1                     -0.585898                      0.000133  1.094277   \n",
       "2                     -0.851135                      0.000077  0.962326   \n",
       "3                     -0.542126                      0.000351  0.891039   \n",
       "4                      0.000000                      0.000442  1.097319   \n",
       "..                          ...                           ...       ...   \n",
       "96                    -0.712466                      0.007395  0.789207   \n",
       "97                    -0.368860                      0.017545  0.823860   \n",
       "98                    -0.708743                      0.000805  0.856094   \n",
       "99                    -0.893389                      0.001636  0.783548   \n",
       "100                   -0.175111                      0.000817  1.036170   \n",
       "\n",
       "     HRV_SampEn  HRV_ShanEn  HRV_MSEn  HRV_CMSEn   HRV_HFD   HRV_KFD   HRV_LZC  \n",
       "0      1.662548    7.103751  1.229793   1.392336  1.965384  4.166548  1.197715  \n",
       "1      1.868132    7.555224  1.174007   1.288954  1.967041  2.788036  1.117846  \n",
       "2      1.746639    7.316140  1.387296   1.315389  1.939231  4.690344  1.122028  \n",
       "3      1.377711    7.143425  1.196983   1.302091  1.974733  2.787734  1.051012  \n",
       "4      1.668343    7.084940  1.458232   1.302750  1.984928  2.193969  1.162819  \n",
       "..          ...         ...       ...        ...       ...       ...       ...  \n",
       "96     0.678190    6.976664  1.056822   1.177499  1.907768  2.744730  0.736001  \n",
       "97     0.770202    6.889927  0.837755   1.038622  1.897684  1.796485  1.018469  \n",
       "98     0.805586    7.184780  0.978753   1.191947  1.932554  2.855720  0.763747  \n",
       "99     0.744972    6.726446  0.474042   1.009792  1.917123  1.941572  0.839453  \n",
       "100    1.528139    6.962487  1.326621   1.389550  1.857610  2.694537  0.890041  \n",
       "\n",
       "[101 rows x 40 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = (y >= 16).astype(int)\n",
    "y.name = 'IsAnxious'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      0\n",
       "1      1\n",
       "2      0\n",
       "3      0\n",
       "4      0\n",
       "      ..\n",
       "96     1\n",
       "97     1\n",
       "98     1\n",
       "99     1\n",
       "100    0\n",
       "Name: IsAnxious, Length: 101, dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 12 candidates, totalling 60 fits\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 216 candidates, totalling 1080 fits\n",
      "Fitting 5 folds for each of 6 candidates, totalling 30 fits\n",
      "Fitting 5 folds for each of 18 candidates, totalling 90 fits\n",
      "Fitting 5 folds for each of 72 candidates, totalling 360 fits\n",
      "Fitting 5 folds for each of 1 candidates, totalling 5 fits\n",
      "Fitting 5 folds for each of 1 candidates, totalling 5 fits\n",
      "Model: svc\n",
      "Best parameters: {'classifier__C': 0.1, 'classifier__gamma': 'scale', 'classifier__kernel': 'rbf'}\n",
      "Best cross-validation score: 0.5742857142857142\n",
      "Best standard deviation: 0.022406267344797285\n",
      "--------------------------------------------------\n",
      "Model: rfc\n",
      "Best parameters: {'classifier__max_depth': None, 'classifier__max_features': 'sqrt', 'classifier__min_samples_leaf': 4, 'classifier__min_samples_split': 2, 'classifier__n_estimators': 50}\n",
      "Best cross-validation score: 0.5147619047619048\n",
      "Best standard deviation: 0.04894348631439845\n",
      "--------------------------------------------------\n",
      "Model: logreg\n",
      "Best parameters: {'classifier__C': 0.1, 'classifier__solver': 'lbfgs'}\n",
      "Best cross-validation score: 0.5547619047619048\n",
      "Best standard deviation: 0.0872611739071583\n",
      "--------------------------------------------------\n",
      "Model: xgboost\n",
      "Best parameters: {'classifier__learning_rate': 0.01, 'classifier__max_depth': 5, 'classifier__n_estimators': 100}\n",
      "Best cross-validation score: 0.4057142857142857\n",
      "Best standard deviation: 0.03362457798839949\n",
      "--------------------------------------------------\n",
      "Model: dtc\n",
      "Best parameters: {'classifier__max_depth': None, 'classifier__max_features': 'sqrt', 'classifier__min_samples_leaf': 4, 'classifier__min_samples_split': 10}\n",
      "Best cross-validation score: 0.5047619047619049\n",
      "Best standard deviation: 0.03302579216076135\n",
      "--------------------------------------------------\n",
      "Model: nbayes\n",
      "Best parameters: {}\n",
      "Best cross-validation score: 0.4352380952380952\n",
      "Best standard deviation: 0.07013753899601292\n",
      "--------------------------------------------------\n",
      "Model: voting-algorithm\n",
      "Best parameters: {}\n",
      "Best cross-validation score: 0.5238095238095238\n",
      "Best standard deviation: 0.07916800929760154\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "def create_pipeline(classifier):\n",
    "    return Pipeline([\n",
    "        ('imputer', KNNImputer(n_neighbors=5)),  # Replace missing values with the mean\n",
    "        ('scaler', RobustScaler()),  # Scale features using RobustScaler\n",
    "        ('kbest', SelectKBest(score_func=f_classif, k=15)),  # Select features using SelectKBest\n",
    "        ('classifier', classifier),  # Classifier\n",
    "    ])\n",
    "\n",
    "# Define the parameter grid for GridSearchCV\n",
    "param_grids = {\n",
    "    'svc': {\n",
    "        'classifier__kernel': ['linear', 'rbf'],\n",
    "        'classifier__C': [0.1, 1.0, 10.0],\n",
    "        'classifier__gamma': ['scale', 'auto']\n",
    "    },\n",
    "    'rfc': {\n",
    "        'classifier': [RandomForestClassifier(random_state=42)],\n",
    "        'classifier__n_estimators': [50, 100, 200],\n",
    "        'classifier__max_depth': [None, 10, 20, 30],\n",
    "        'classifier__min_samples_split': [2, 5, 10],\n",
    "        'classifier__min_samples_leaf': [1, 2, 4],\n",
    "        'classifier__max_features': ['sqrt', 'log2']\n",
    "    },\n",
    "    'logreg': {\n",
    "        'classifier': [LogisticRegression(random_state=42)],\n",
    "        'classifier__solver': ['lbfgs', 'liblinear'],\n",
    "        'classifier__C': [0.1, 1.0, 10.0]\n",
    "    },\n",
    "    'xgboost': {\n",
    "        'classifier': [XGBClassifier(random_state=42)],\n",
    "        'classifier__learning_rate': [0.1, 0.01],\n",
    "        'classifier__n_estimators': [100, 200, 300],\n",
    "        'classifier__max_depth': [3, 5, 7]\n",
    "    },\n",
    "    'dtc': {\n",
    "        'classifier': [DecisionTreeClassifier(random_state=42)],\n",
    "        'classifier__max_depth': [None, 10, 20, 30],\n",
    "        'classifier__min_samples_split': [2, 5, 10],\n",
    "        'classifier__min_samples_leaf': [1, 2, 4],\n",
    "        'classifier__max_features': ['sqrt', 'log2']\n",
    "    },\n",
    "    'nbayes': {\n",
    "        'classifier': [GaussianNB()]\n",
    "    },\n",
    "    'voting-algorithm': {\n",
    "        'classifier': [VotingClassifier(estimators=[('svc', SVC(random_state=42))])]\n",
    "    }\n",
    "}\n",
    "\n",
    "# Create StratifiedKFold for cross-validation\n",
    "skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\n",
    "\n",
    "# Perform GridSearchCV for each classifier\n",
    "results = {}\n",
    "for model_name, param_grid in param_grids.items():\n",
    "    if 'classifier' in param_grid:\n",
    "        classifier = param_grid['classifier'][0]\n",
    "        del param_grid['classifier']\n",
    "        pipeline = create_pipeline(classifier)\n",
    "    else:\n",
    "        pipeline = create_pipeline(SVC(random_state=42))\n",
    "    \n",
    "    grid_search = GridSearchCV(pipeline, param_grid, cv=skf, scoring='accuracy', verbose=1, n_jobs=-1)\n",
    "    grid_search.fit(X, y)  # Assuming X and y are your features and target variables\n",
    "    results[model_name] = {\n",
    "        'best_params': grid_search.best_params_,\n",
    "        'best_score': grid_search.best_score_,\n",
    "        'best_std_deviation': grid_search.cv_results_['std_test_score'][grid_search.best_index_]\n",
    "    }\n",
    "\n",
    "# Print results for each model\n",
    "for model_name, result in results.items():\n",
    "    print(f\"Model: {model_name}\")\n",
    "    print(\"Best parameters:\", result['best_params'])\n",
    "    print(\"Best cross-validation score:\", result['best_score'])\n",
    "    print(\"Best standard deviation:\", result['best_std_deviation'])\n",
    "    print(\"-\" * 50)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
